package vy.org.app.page.member;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.vy.dal.dao.ViewVyLookupPatrikaPeriodDAO;
import org.vy.dal.dao.ViewVyLookupPatrikaPeriodDO;
import org.vy.dal.dao.VyAddressDAO;
import org.vy.dal.dao.VyAddressDO;
import org.vy.dal.dao.VyDabbaSubscriptionDAO;
import org.vy.dal.dao.VyDabbaSubscriptionDO;
import org.vy.dal.dao.VyMagazineSubscriptionDAO;
import org.vy.dal.dao.VyMagazineSubscriptionDO;
import org.vy.dal.dao.VyMemberDAO;
import org.vy.dal.dao.VyMemberDO;
import org.vy.dal.dao.lookup.ViewVyLookupCitiesDAO;
import org.vy.dal.dao.lookup.ViewVyLookupCitiesDO;
import org.vy.dal.dao.lookup.VyLookupDeliveryModeDAO;
import org.vy.dal.dao.lookup.VyLookupDeliveryModeDO;
import org.vy.dal.dao.lookup.VyLookupPatrikaDAO;
import org.vy.dal.dao.lookup.VyLookupPatrikaDO;

import vy.org.app.common.AppConstants;
import vy.org.app.common.BaseAction;
import vy.org.app.common.IdTypeEnum;
import vy.org.app.common.ModuleEnum;
import vy.org.app.page.lookup.delivery.DeliveryModeHelper;
import vy.org.app.page.lookup.delivery.MntnDeliveryModeForm;
import vy.org.app.page.lookup.patrika.MntnPatrikaPeriodForm;
import vy.org.app.page.lookup.patrika.PatrikaHelper;
import vy.org.app.page.subscription.dabba.DabbaForm;
import vy.org.app.page.subscription.magazine.MagazineForm;

public class EditMemberAction extends BaseAction {

	@Override
	public ActionForward executeAction(ActionMapping mapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) {
		
		MemberForm form = (MemberForm)actionForm;
		String vyId = request.getParameter(AppConstants.VY_ID);
		if(vyId == null)
			vyId = form.getVyId();
		
		if(vyId == null || "".equals(vyId) || form == null){
			throw new IllegalArgumentException("Modules accessed with wrong params. An event for this has been logged");
		}
		
		try{
			List<VyLookupPatrikaDO> patrikaLst = VyLookupPatrikaDAO.findAllActive();
			List<ViewVyLookupPatrikaPeriodDO> patPeriodLst = ViewVyLookupPatrikaPeriodDAO.findAllActive();
			List<VyLookupDeliveryModeDO> deliveryModeLst = VyLookupDeliveryModeDAO.findAllActive();
			
			Map<String, List<MntnPatrikaPeriodForm>> patrikaMap = new HashMap<String, List<MntnPatrikaPeriodForm>>(); 
			Map<String, MntnDeliveryModeForm> delModeMap = new HashMap<String, MntnDeliveryModeForm>(); 
			
			for(VyLookupPatrikaDO pat : patrikaLst){
				patrikaMap.put(pat.getPatrikaId().toString(), PatrikaHelper.fillPatrikaPeriodForm(pat, patPeriodLst));
			}
			for(VyLookupDeliveryModeDO dl : deliveryModeLst){
				delModeMap.put(dl.getDeliveryModeCode(), DeliveryModeHelper.fillPatrikaForm(dl));
			}
			form.setPatrikas(patrikaMap);
			form.setDeliveryModes(delModeMap);

		}catch(Exception e){
			e.printStackTrace();
		}

		try{
			VyMemberDO mem = (VyMemberDO)VyMemberDAO.findByPK(vyId);
			List<VyAddressDO> addrLst = new ArrayList<VyAddressDO>();
			List<ViewVyLookupCitiesDO> countryLst = ViewVyLookupCitiesDAO.findUniqueCountries();
			List<ViewVyLookupCitiesDO> stateLst1=null,stateLst2=null,stateLst3=null,cityLst1=null,cityLst2=null,cityLst3=null;

			if(mem != null){
				if(mem.getPresentAddressId() != null){
					VyAddressDO prtAddr = (VyAddressDO)VyAddressDAO.findByPK(mem.getPresentAddressId());
					if(prtAddr != null){
						addrLst.add(prtAddr);
						if(prtAddr.getCountry() != null && !"".equals(prtAddr.getCountry()))
							stateLst1 = ViewVyLookupCitiesDAO.findStateByCountry(prtAddr.getCountry());
						if(prtAddr.getStateOrRegion() != null && !"".equals(prtAddr.getStateOrRegion()))
							cityLst1 = ViewVyLookupCitiesDAO.findCitiesByStateAndCountry(prtAddr.getStateOrRegion(), prtAddr.getCountry());
					}
				}
				if(mem.getPermanentAddressId() != null){
					VyAddressDO pmtAddr = (VyAddressDO)VyAddressDAO.findByPK(mem.getPermanentAddressId());
					if(pmtAddr != null){
						addrLst.add(pmtAddr);
						if(pmtAddr.getCountry() != null && !"".equals(pmtAddr.getCountry()))
							stateLst2 = ViewVyLookupCitiesDAO.findStateByCountry(pmtAddr.getCountry());
						if(pmtAddr.getStateOrRegion() != null && !"".equals(pmtAddr.getStateOrRegion()))
							cityLst2 = ViewVyLookupCitiesDAO.findCitiesByStateAndCountry(pmtAddr.getStateOrRegion(), pmtAddr.getCountry());
					}
				}
				if(mem.getOfficeAddressId() != null){
					VyAddressDO offAddr = (VyAddressDO)VyAddressDAO.findByPK(mem.getOfficeAddressId());
					if(offAddr != null){
						addrLst.add(offAddr);
						if(offAddr.getCountry() != null && !"".equals(offAddr.getCountry()))
							stateLst3 = ViewVyLookupCitiesDAO.findStateByCountry(offAddr.getCountry());
						if(offAddr.getStateOrRegion() != null && !"".equals(offAddr.getStateOrRegion()))
							cityLst3 = ViewVyLookupCitiesDAO.findCitiesByStateAndCountry(offAddr.getStateOrRegion(), offAddr.getCountry());
					}
				}
				List<VyDabbaSubscriptionDO> dabbaList = VyDabbaSubscriptionDAO.findActiveById(vyId);
				List<VyMagazineSubscriptionDO> magazineList = VyMagazineSubscriptionDAO.findActiveByVyId(vyId);
				
				MemberHelper.fillMemberForm(form, mem, addrLst, magazineList, dabbaList);
				form.getPresentAddress().setCountries(countryLst);
				form.getPermanentAddress().setCountries(countryLst);
				form.getOfficeAddress().setCountries(countryLst);
				form.getPresentAddress().setStates(stateLst1);
				form.getPermanentAddress().setStates(stateLst2);
				form.getOfficeAddress().setStates(stateLst3);
				form.getPresentAddress().setCities(cityLst1);
				form.getPermanentAddress().setCities(cityLst2);
				form.getOfficeAddress().setCities(cityLst3);
			}

			if(ModuleEnum.ADD_SUBSCRIPTION.getName().equals(form.getMode())){
				List<MagazineForm> mzForms = null;
				if(form.getMagazines() != null)
					mzForms = form.getMagazines();
				else
					mzForms = new ArrayList<MagazineForm>();

				MagazineForm mzForm = new MagazineForm();
				mzForm.setVyId(form.getVyId());
				mzForms.add(mzForm);
				form.setMagazines(mzForms);
				form.setMagazineSubscriptionCnt(form.getMagazineSubscriptionCnt()+1);
				return mapping.findForward("failure");
			}
			
			if(ModuleEnum.ADD_PATRA.getName().equals(form.getMode())){
				List<DabbaForm> daForms = null;
				if(form.getDabbas() != null)
					daForms = form.getDabbas();
				else
					daForms = new ArrayList<DabbaForm>();

				DabbaForm daForm = new DabbaForm();
				daForm.setId(form.getVyId());
				daForm.setIdType(IdTypeEnum.MEMBER.getId().toString());
				daForms.add(daForm);
				form.setDabbas(daForms);
				form.setDabbaSubscriptionCnt(form.getDabbaSubscriptionCnt()+1);
				return mapping.findForward("failure");
			}

		}catch(Exception e) {
			e.printStackTrace();
			return mapping.findForward("failure");
		}		
		return mapping.findForward("success");
	}
	
	@Override
	protected ModuleEnum getModuleId() {
		return ModuleEnum.ADD_MEMBER;
	}
	
}
